#include <qdebug.h>
#include <QRect>
#include "keyboard.h"
#include "inputmethod.h"
#include "ui_keyboard.h"
#include <QWidget>

keyboard::keyboard(INPUTMethod *ipm,QWidget *parent) :
    QWidget(parent, Qt::Tool| Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint),
    ui(new Ui::keyboard)
{

    qDebug()<<"keyboard() start";
    ui->setupUi(this);

    //this->setStyleSheet("QPushButton{border:2px groove gray;border-radius:10px;padding:2px 4px; pressed:background-color: rgb(250, 148, 27);}");
    this->setStyleSheet("QPushButton{border:2px groove gray;border-radius:4px;padding:2px 2px;background-color: rgb(255, 255, 255);}");// background-color: rgb(255, 255, 255);
    //ui->key_Cap_Lock->setStyleSheet("QPushButton{border:2px groove red;border-radius:5px;padding:2px 2px;}");
    //ui->clearBtn->setStyleSheet("QPushButton{border:2px groove red;border-radius:5px;padding:2px 2px;}");

    btnKeys = new QButtonGroup;
    SetKeyBoard();
    QObject::connect(btnKeys,SIGNAL(buttonClicked (int)),this,SLOT(keyPressed(int)));
    QObject::connect(this,SIGNAL(SendInputMessage(char)),ipm,SLOT(PreeditContent(char)));

    QObject::connect(this,SIGNAL(SenEscape()),ipm,SLOT(Escape()));

    btnBar = new QButtonGroup;
    SetKeyBar();
    setBar(0,false);
    ui->listDown->setVisible (false);
    ui->listUp->setVisible (false);
    QObject::connect(btnBar,SIGNAL(buttonClicked (int)),this,SLOT(barPressed(int)));

    method = english;
    moveAble = false;
//    this->setMouseTracking(true);

       // 标点符号输入面板,下面三句代码是控制软键盘弹出的时候是英文小写输入模式
//    CapLockFlag = false;
//    ShiftFlag = true;
//    CharFlage = false;

    /* 标点符号输入面板,下面三句代码是控制软键盘弹出的时候是数字和字符的输入模式.
     * 配合 initKeyBoard() 这个初始化函数使用就行，如果以后不需要这样的定制功能，
     * 就直接删除这几行代码和这个函数; 并且打开上面的 “英文小写输入模式”的三行代码的注释
     */
    CapLockFlag = false;
    ShiftFlag = false;  // 标点符号输入面板
    CharFlage = false;

   // setKeyValue();
    setKeyValueForCaplock();
    firstFlag = ' ';

//    setStyleSheet ("background-image: url(:/src/keyboarbackground.JPG);");
//    initpos();
    changeSize();

    // 配合 CapLockFlag, ShiftFlag, CharFlage 几个标志变量使用的，实现软键盘弹出时就是数字和字符的输入模式
    initKeyBoard();
}

keyboard::~keyboard()
{
    delete btnKeys;
    delete btnBar;
    delete ui;
}

/* 这个函数是根据项目需求临时添加的，有点破坏整个工程的代码美感!
 * 因为要实现软键盘弹出时就是数字和字符的输入模式，为了不破坏原有的代码框架，
 * 我觉得还是多写点代码好了，不过这样做也就显得有点臃肿了
*/
void  keyboard::initKeyBoard()
{
#if  1
    // 把大小写按键的背景颜色设置为橙色
    ui->key_Shift->setStyleSheet("QPushButton{background-color:rgb(250, 148, 27);}");// 橙色  rgb(35, 192, 220)

    // 全键盘输入面板的第一行按键
    ui->key_Q->setText ("0");
    ui->key_W->setText ("1");
    ui->key_E->setText ("2");
    ui->key_R->setText ("3");
    ui->key_T->setText ("4");
    ui->key_Y->setText ("5");
    ui->key_U->setText ("6");
    ui->key_I->setText ("7");
    ui->key_O->setText ("8");
    ui->key_P->setText ("9");

    // 全键盘输入面板的第二行按键
    ui->key_A->setText ( "(");
    ui->key_S->setText ( ")");
    ui->key_D->setText ( "@");
    ui->key_F->setText ( "#");
    ui->key_G->setText ( "=");
    ui->key_H->setText ( "%");
    ui->key_J->setText ( "^");
    ui->key_K->setText ( "-");
    ui->key_L->setText ( "+");

    // 全键盘输入面板的第三行按键

    ui->key_Z->setText ( "!");
    ui->key_X->setText ("|");
    ui->key_C->setText ( ":");
    //ui->key_C->setText ( "_");
    ui->key_V->setText ( "*");
    ui->key_B->setText ( "[");
    ui->key_N->setText ( "]");
    ui->key_M->setText ( "~");

#endif


}


// 这里不知道为什么没反应，根本就无法进入这个按键事件的函数
void  keyboard::keyPressEvent ( QKeyEvent  *e)
{
    qDebug()<<"key A was pressed";
    if( e->key() == Qt::Key_A )
    {
        ui->key_A->setStyleSheet("background-color: rgb(250, 148, 27);");

    }
}
#if 0
void keyboard::mousePressEvent (QMouseEvent *event)
{

    if (event->button() != Qt::LeftButton) return;
    moveAble = true;
    widget_pos = pos();
    mouse_pos = event->globalPos();

}
void keyboard::mouseReleaseEvent(QMouseEvent *event)
{

        if (event->button() != Qt::LeftButton) return;
        moveAble = false;

}
void keyboard::mouseMoveEvent ( QMouseEvent * event )
{


    if (moveAble)
    {
        QPoint pos = event->globalPos();
        int x = pos.x() - mouse_pos.x();
        int y = pos.y() - mouse_pos.y();
        QWidget::move(widget_pos.x() + x,widget_pos.y() + y);
    }

}
#else
void keyboard::mousePressEvent(QMouseEvent * event)
{
    if (event->button() == Qt::LeftButton)
    {
         dragPosition = event->globalPos() - frameGeometry().topLeft();
         moveAble = true;
         event->accept();
    }
}


/*
 * 鼠标移动事件的处理:
 * 已经优化，软键盘只能在LCD的上下两个方向移动，并且上下移动的
 * 范围也已经做了一定的限制，这样做可以让用户获得更好的使用体验！
 */
void keyboard::mouseMoveEvent(QMouseEvent * event)
{
    if ((event->buttons() & Qt::LeftButton) && moveAble)
    {

        // 下面两句是原来的代码,可以实现输入法面板的上下、左右移动
         //move(event->globalPos() - dragPosition);
         //dragPosition = event->globalPos() - frameGeometry().topLeft();

        /* 修改为下面的代码之后,限制了输入法面板的左右移动，也就是说现在只能上下移动了，
         * 这样就避免了用户把输入法到处移动的尴尬
         */
        int posy = event->globalPos().y()- dragPosition.y(); // 获取软键盘在LCD上应该移动的高度

        if( posy <= 30 )    // 限制 LCD 上软键盘往上面移动的高度
        {
            posy = 30;
        }
        if( posy >= 526)    //  限制 LCD 上软键盘往下面移动的高度
        {       // 800 - 284 = 526 , 即LCD的分辨率高度 - 软键盘高度  = 限制软键盘显示位置的高度

            posy = 516;
        }


        move(0, posy);//强行将move的第一个参数设置为0，将无法再实现左右移动了
        dragPosition = event->globalPos() - frameGeometry().topLeft();

         event->accept();
    }
}

void keyboard::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button()!=Qt::LeftButton) return;
    moveAble = false;

}
#endif

void keyboard::SetKeyBar()
{
    btnBar->addButton (ui->font_1,Qt::Key_0);
    btnBar->addButton (ui->font_2,Qt::Key_1);
    btnBar->addButton (ui->font_3,Qt::Key_2);
    btnBar->addButton (ui->font_4,Qt::Key_3);
    btnBar->addButton (ui->font_5,Qt::Key_4);
    btnBar->addButton (ui->font_6,Qt::Key_5);
    btnBar->addButton (ui->font_7,Qt::Key_6);
    btnBar->addButton (ui->font_8,Qt::Key_7);
    btnBar->addButton (ui->font_9,Qt::Key_8);
    btnBar->addButton (ui->font_10,Qt::Key_9);
    btnBar->addButton (ui->listUp,Qt::Key_Up);
    btnBar->addButton (ui->listDown,Qt::Key_Down);
}

void keyboard::SetKeyBoard()
{

    //  修改1
//    btnKeys->addButton(ui->key_0,Qt::Key_0);
//    btnKeys->addButton(ui->key_1,Qt::Key_1);
//    btnKeys->addButton(ui->key_2,Qt::Key_2);
//    btnKeys->addButton(ui->key_3,Qt::Key_3);
//    btnKeys->addButton(ui->key_4,Qt::Key_4);
//    btnKeys->addButton(ui->key_5,Qt::Key_5);
//    btnKeys->addButton(ui->key_6,Qt::Key_6);
//    btnKeys->addButton(ui->key_7,Qt::Key_7);
//    btnKeys->addButton(ui->key_8,Qt::Key_8);
//    btnKeys->addButton(ui->key_9,Qt::Key_9);

    btnKeys->addButton(ui->key_A,Qt::Key_A);
    btnKeys->addButton(ui->key_B,Qt::Key_B);
    btnKeys->addButton(ui->key_C,Qt::Key_C);
    btnKeys->addButton(ui->key_D,Qt::Key_D);
    btnKeys->addButton(ui->key_E,Qt::Key_E);
    btnKeys->addButton(ui->key_F,Qt::Key_F);
    btnKeys->addButton(ui->key_G,Qt::Key_G);
    btnKeys->addButton(ui->key_H,Qt::Key_H);
    btnKeys->addButton(ui->key_I,Qt::Key_I);
    btnKeys->addButton(ui->key_J,Qt::Key_J);
    btnKeys->addButton(ui->key_K,Qt::Key_K);
    btnKeys->addButton(ui->key_L,Qt::Key_L);
    btnKeys->addButton(ui->key_M,Qt::Key_M);
    btnKeys->addButton(ui->key_N,Qt::Key_N);
    btnKeys->addButton(ui->key_O,Qt::Key_O);
    btnKeys->addButton(ui->key_P,Qt::Key_P);
    btnKeys->addButton(ui->key_Q,Qt::Key_Q);
    btnKeys->addButton(ui->key_R,Qt::Key_R);
    btnKeys->addButton(ui->key_S,Qt::Key_S);
    btnKeys->addButton(ui->key_T,Qt::Key_T);
    btnKeys->addButton(ui->key_U,Qt::Key_U);
    btnKeys->addButton(ui->key_V,Qt::Key_V);
    btnKeys->addButton(ui->key_W,Qt::Key_W);
    btnKeys->addButton(ui->key_X,Qt::Key_X);
    btnKeys->addButton(ui->key_Y,Qt::Key_Y);
    btnKeys->addButton(ui->key_Z,Qt::Key_Z);

    btnKeys->addButton(ui->Key_Comma, Qt::Key_Comma); //,
    btnKeys->addButton(ui->Key_Period, Qt::Key_Period);  //.
    btnKeys->addButton(ui->Key_Slash, Qt::Key_Slash);
//    btnKeys->addButton(ui->Key_rslash,Qt::Key_Romaji);
//    btnKeys->addButton(ui->Key_Colon, Qt::Key_Colon);  //:

    btnKeys->addButton(ui->key_Space,Qt::Key_Space);
    btnKeys->addButton(ui->key_Enter,Qt::Key_Enter);
    btnKeys->addButton(ui->key_Backspace,Qt::Key_Backspace);
//    btnKeys->addButton(ui->key_Escape,Qt::Key_Escape);
    btnKeys->addButton(ui->key_Cap_Lock,Qt::Key_CapsLock);
    btnKeys->addButton(ui->key_LangueChange,Qt::Key_Control);

//    btnKeys->addButton (ui->Key_left,Qt::Key_Left);
//    btnKeys->addButton (ui->Key_right,Qt::Key_Right);
//    btnKeys->addButton (ui->Key_up,Qt::Key_Up);
//    btnKeys->addButton (ui->Key_down,Qt::Key_Down);

     //  修改 ②
//    btnKeys->addButton (ui->key_minus,Qt::Key_Minus); //-
//    btnKeys->addButton (ui->key_equal,Qt::Key_Equal); //=
//    btnKeys->addButton (ui->key_braceleft,Qt::Key_BraceLeft); //{
//    btnKeys->addButton (ui->key_braceright,Qt::Key_BraceRight); //}
    btnKeys->addButton (ui->Key_quotation,Qt::Key_QuoteDbl); //'
//    btnKeys->addButton (ui->Key_xx,Qt::Key_acute);  //`
    btnKeys->addButton (ui->Key_Close,Qt::Key_Close);
    btnKeys->addButton (ui->key_Shift,Qt::Key_Shift);
}

void keyboard::barPressed(int id )
{
    QString str;
    QWidget * widget;

    switch(id)
    {
    case Qt::Key_0 :  str = ui->font_1->text (); break;
    case Qt::Key_1 :  str = ui->font_2->text (); break;
    case Qt::Key_2 :  str = ui->font_3->text (); break;
    case Qt::Key_3 :  str = ui->font_4->text (); break;
    case Qt::Key_4 :  str = ui->font_5->text (); break;
    case Qt::Key_5 :  str = ui->font_6->text (); break;
    case Qt::Key_6 :  str = ui->font_7->text (); break;
    case Qt::Key_7 :  str = ui->font_8->text (); break;
    case Qt::Key_8 :  str = ui->font_9->text (); break;
    case Qt::Key_9 :  str = ui->font_10->text (); break;
    case Qt::Key_Up:
        if(currentPage)
        {
            currentPage --;
            showPage(currentPage);
        }
        return;
    case Qt::Key_Down:
        if((currentPage + 1) < pageCount)
        {
            currentPage ++;
            showPage(currentPage);
        }
        return;
    }
    widget = QApplication::focusWidget();

    QLineEdit *qle = qobject_cast<QLineEdit*>( widget);
    if(qle == 0)
    {
        QTextEdit *qte = qobject_cast<QTextEdit*>( widget);
        if(qte == 0)
        {
            QPlainTextEdit * qpte = qobject_cast<QPlainTextEdit*>( widget);
            if(qpte == 0)
            {
                QTextBrowser *qtb = qobject_cast<QTextBrowser*>( widget);
                if(qtb != 0) qtb->insertPlainText (str);
            }
            else qpte->insertPlainText (str);
        }
        else   qte->insertPlainText (str);
    }
    else qle->insert (str);

    setBar(0,false);
    clearBar();
}


// 这里实现了第三个输入面板:符号输入面板
// 设置输入面板字符输入状态时按键所对应的字符值
// 这个符号输入面板是由我添加和修改的
void  keyboard::setCharValue(int keyId)
{
    char c ,nustr[] = { '(', '[', ':', '@', '2', '#', '=', '%', '7', '^', '-', '+',
                        '~', ']', '8', '9', '0', '3', ')', '4', '6', '*', '1', '|',
                        '5', '!'
                      };

    switch( keyId )
    {
        case Qt::Key_A...Qt::Key_Z:
            c = keyId - Qt::Key_A;
            c = *(nustr + c);
            emit SendInputMessage(c);
            break;
        default:
            break;
    }


// ///////////////////////////////////////////
#if  0
    // 全键盘输入面板的第一行按键
    ui->key_Q->setText ("0");
    ui->key_W->setText ("1");
    ui->key_E->setText ("2");
    ui->key_R->setText ("3");
    ui->key_T->setText ("4");
    ui->key_Y->setText ("5");
    ui->key_U->setText ("6");
    ui->key_I->setText ("7");
    ui->key_O->setText ("8");
    ui->key_P->setText ("9");

    // 全键盘输入面板的第二行按键
    ui->key_A->setText ( "(");
    ui->key_S->setText ( ")");
    ui->key_D->setText ( "@");
    ui->key_F->setText ( "#");
    ui->key_G->setText ( "$");
    ui->key_H->setText ( "%");
    ui->key_J->setText ( "^");
    ui->key_K->setText ( "&");
    ui->key_L->setText ( "*");

    // 全键盘输入面板的第三行按键

    ui->key_Z->setText ( "!");
    ui->key_X->setText ("|");
    ui->key_C->setText ( ":");
    //ui->key_C->setText ( "_");
    ui->key_V->setText ( "+");
    ui->key_B->setText ( "{");
    ui->key_N->setText ( "}");
    ui->key_M->setText ( "~");

#endif

}


void keyboard::keyPressed(int id )
{
    QKeyEvent *key;
    char c ,nustr[] = {')','!','@','#','=','%','^','&','*','('};

    switch (id)
    {
    case Qt::Key_0...Qt::Key_9:
          c = id - Qt::Key_0;
          if(ShiftFlag) c = *(nustr + c);//大写
          else c += '0';
          if( (ShiftFlag) || ((method == wuhua) && (c > '5')) || ((!ShiftFlag) && (method != wuhua)) )
          {
            emit SendInputMessage(c);
          }
          else //五笔划输入法编码
          {
              QString ch;
              ch.clear ();
              ch.append (c);
              transPy(ch);
          }
        break;

    case Qt::Key_A...Qt::Key_Z:
        if(  ! ShiftFlag )
        {
            setCharValue(id);
        }

        else
        {

            c = 'a' + id - Qt::Key_A;
            if( CapLockFlag  )  //Cap Locked
            {
              c -= 0x20;
              emit SendInputMessage(c);
            }
                else
                {
                  if((method == english) || (method == wuhua) || (CapLockFlag)
                     || ((method == wubi) && (c == 'z')))  //英文、笔划输入法或大写或五笔输入法按键为z
                  {
                     emit SendInputMessage(c);
                  }
                  else  //拼音、五笔输入法编码
                  {
                    QString ch;
                    ch.clear ();
                    ch.append (c);
                    transPy(ch);
                  }
                }

        }


#if  0
        c = 'a' + id - Qt::Key_A;
        if( CapLockFlag || ((!CapLockFlag) && (ShiftFlag)))  //Cap Locked
        {
          if(ShiftFlag ^ CapLockFlag) c -= 0x20;
          emit SendInputMessage(c);
        }
        else
        {
          if((method == english) || (method == wuhua) || (CapLockFlag)
             || ((method == wubi) && (c == 'z')))  //英文、笔划输入法或大写或五笔输入法按键为z
          {
             emit SendInputMessage(c);
          }
          else  //拼音、五笔输入法编码
          {
            QString ch;
            ch.clear ();
            ch.append (c);
            transPy(ch);
          }
        }
#endif
        break;

    case Qt::Key_Space :  //空格
        if(ui->font_1->isVisible ())  //有可选汉字输入
        {
            barPressed(Qt::Key_0);
        }
        else
        {
            emit SendInputMessage(' ');
        }
        break;

    case Qt::Key_Backspace :
        if((method == english) || ((method != english) && (ui->lineEdit->text ().isEmpty ())))
        {
            key = new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
            QCoreApplication::postEvent(QApplication::focusWidget(), key);
        }
        else   //编码输入处理
        {
            QByteArray text;
            int len;
            text = /*bar->m_*/ui->lineEdit->text ().toAscii ();
            len = strlen(text);
            if(len)
            {
                text[len - 1] = 0;
                if(len == 1) {
                   ui->lineEdit->clear();
                   clearBar  ();
                }
                else {
                  ui->lineEdit->setText (text);
                  transPy("");
               }
            }
        }
        break;

    case Qt::Key_Enter :
        if((method == english) || ((method != english) && (ui->lineEdit->text ().isEmpty ())))
        {
            key = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
            QCoreApplication::postEvent(QApplication::focusWidget(), key);
        }
        else  //中文输入编码作为字符串输入并清除
        {
            clearBar ();
//            setBar(0,false);
        }
        break;
    case Qt::Key_Escape :
        key = new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);
        QCoreApplication::postEvent(QApplication::focusWidget(), key);
//        emit SenEscape();
        break;
    case Qt::Key_CapsLock :
        if(CapLockFlag) CapLockFlag = false;
        else CapLockFlag = true;
        ShiftFlag = true;
        //if( ! ShiftFlag ) ShiftFlag = true;
        setKeyValue( id );


        break;
    case Qt::Key_Shift:

       //暂时注释，准备把这个步骤放在其他的函数

        if( ShiftFlag ) ShiftFlag = false;
        else ShiftFlag = true;
        setKeyValue( id );
        break;
    case Qt::Key_Control :  //languageChange
        changeInputMethod();
        break;
    case Qt::Key_Left:
    case Qt::Key_Right:
    case Qt::Key_Up:
    case Qt::Key_Down:
        break;
    case Qt::Key_Comma : //逗号
        emit SendInputMessage(ShiftFlag? '<':',');
        break;
    case Qt::Key_Period : //小数点
        emit SendInputMessage(ShiftFlag? '>':'.');
        break;
    case Qt::Key_Slash :  //左斜杠
        emit SendInputMessage(ShiftFlag? '?':'/');
        break;
    case Qt::Key_Colon :  //冒号
        emit SendInputMessage(ShiftFlag? ':':';');
        break;
    case Qt::Key_Minus:
        emit SendInputMessage(ShiftFlag? '_':'-');
        break;
    case Qt::Key_Equal:
        emit SendInputMessage(ShiftFlag? '+':'=');
        break;
    case Qt::Key_BraceLeft:
        emit SendInputMessage(ShiftFlag? '{':'[');
        break;
    case Qt::Key_BraceRight:
        emit SendInputMessage(ShiftFlag? '}':']');
        break;
    case Qt::Key_QuoteDbl: //'
        emit SendInputMessage(ShiftFlag? '\"':'\'');
        break;
    case Qt::Key_Romaji: //反斜杠
        emit SendInputMessage(ShiftFlag? '|':'\\');
        break;
    case Qt::Key_acute: //`
        emit SendInputMessage(ShiftFlag? '~':'`');
        break;
    case Qt::Key_Close:
        close ();

    default:
        break;

    }
}

void keyboard::setKeyValueForCaplock()
{

    bool shift_caplock =  CapLockFlag;

    ui->key_A->setText (shift_caplock? "A":"a");
    ui->key_B->setText (shift_caplock? "B":"b");
    ui->key_C->setText (shift_caplock? "C":"c");
    ui->key_D->setText (shift_caplock? "D":"d");
    ui->key_E->setText (shift_caplock? "E":"e");
    ui->key_F->setText (shift_caplock? "F":"f");
    ui->key_G->setText (shift_caplock? "G":"g");
    ui->key_H->setText (shift_caplock? "H":"h");
    ui->key_I->setText (shift_caplock? "I":"i");
    ui->key_J->setText (shift_caplock? "J":"j");
    ui->key_K->setText (shift_caplock? "K":"k");
    ui->key_L->setText (shift_caplock? "L":"l");
    ui->key_M->setText (shift_caplock? "M":"m");
    ui->key_N->setText (shift_caplock? "N":"n");
    ui->key_O->setText (shift_caplock? "O":"o");
    ui->key_P->setText (shift_caplock? "P":"p");
    ui->key_Q->setText (shift_caplock? "Q":"q");
    ui->key_R->setText (shift_caplock? "R":"r");
    ui->key_S->setText (shift_caplock? "S":"s");
    ui->key_T->setText (shift_caplock? "T":"t");
    ui->key_U->setText (shift_caplock? "U":"u");
    ui->key_V->setText (shift_caplock? "V":"v");
    ui->key_W->setText (shift_caplock? "W":"w");
    ui->key_X->setText (shift_caplock? "X":"x");
    ui->key_Y->setText (shift_caplock? "Y":"y");
    ui->key_Z->setText (shift_caplock? "Z":"z");
    ui->Key_Comma->setText (ShiftFlag? "<":",");
    ui->Key_Period->setText (ShiftFlag? ">":".");
    ui->Key_Slash->setText (ShiftFlag? "?":"/");
//    ui->Key_rslash->setText (ShiftFlag? "|":"\\");
//    ui->Key_Colon->setText (ShiftFlag? ":":";");
//    ui->key_minus->setText (ShiftFlag? "_":"-");
//    ui->key_equal->setText (ShiftFlag? "+":"=");
//    ui->key_braceleft->setText (ShiftFlag? "{":"[");
//    ui->key_braceright->setText (ShiftFlag? "}":"]");
    ui->Key_quotation->setText (ShiftFlag? "\"":"\'");
//    ui->Key_xx->setText (ShiftFlag? "~":"`");

}
void keyboard::setKeyValue(int id)
{



        //bool shift_caplock = (ShiftFlag ^ CapLockFlag);
        if((id == Qt::Key_Shift) && (!ShiftFlag))
        {
            // 白色  color: rgb(255, 255, 255);
            ui->key_Shift->setStyleSheet("QPushButton{background-color:rgb(250, 148, 27);}");// 橙色  rgb(35, 192, 220)

            // 把大小写按键的背景颜色设置为白色
            ui->key_Cap_Lock->setStyleSheet("QPushButton{background-color:rgb(255, 255, 255);}");// 橙色  rgb(35, 192, 220)


            //ShiftFlag = false;
            // 全键盘输入面板的第一行按键
            ui->key_Q->setText ("0");
            ui->key_W->setText ("1");
            ui->key_E->setText ("2");
            ui->key_R->setText ("3");
            ui->key_T->setText ("4");
            ui->key_Y->setText ("5");
            ui->key_U->setText ("6");
            ui->key_I->setText ("7");
            ui->key_O->setText ("8");
            ui->key_P->setText ("9");

            // 全键盘输入面板的第二行按键
            ui->key_A->setText ( "(");
            ui->key_S->setText ( ")");
            ui->key_D->setText ( "@");
            ui->key_F->setText ( "#");
            ui->key_G->setText ( "=");
            ui->key_H->setText ( "%");
            ui->key_J->setText ( "^");
            ui->key_K->setText ( "-");
            ui->key_L->setText ( "+");

            // 全键盘输入面板的第三行按键

            ui->key_Z->setText ( "!");
            ui->key_X->setText ("|");
            ui->key_C->setText ( ":");
            //ui->key_C->setText ( "_");
            ui->key_V->setText ( "*");
            ui->key_B->setText ( "[");
            ui->key_N->setText ( "]");
            ui->key_M->setText ( "~");
            ui->Key_Comma->setText(",");
            ui->Key_Period->setText(".");

            ui->Key_quotation->setText ("'");
            ui->Key_Slash->setText ("/");
        //    ui->Key_rslash->setText (ShiftFlag? "|":"\\");
        //    ui->Key_Colon->setText (ShiftFlag? ":":";");
        //    ui->key_minus->setText (ShiftFlag? "_":"-");
        //    ui->key_equal->setText (ShiftFlag? "+":"=");
        //    ui->key_braceleft->setText (ShiftFlag? "{":"[");
        //    ui->key_braceright->setText (ShiftFlag? "}":"]");
        //    ui->Key_quotation->setText (ShiftFlag? "\"":"\'");
        //    ui->Key_xx->setText (ShiftFlag? "~":"`");

        }
        else
        {
            ui->key_Shift->setStyleSheet("QPushButton{background-color:rgb(255, 255, 255);}");// 橙色  rgb(35, 192, 220)


        }



         if( ( (id == Qt::Key_Shift) && (ShiftFlag) ) ||(id == Qt::Key_CapsLock ))
        {


            bool shift_caplock = CapLockFlag;

             // 白色  color: rgb(255, 255, 255);
             //ui->key_Shift->setStyleSheet("QPushButton{background-color:color: rgb(250, 148, 27);}");// 橙色  rgb(250, 148, 27)
            if( shift_caplock )
            {
                // 把大小写按键的背景颜色设置为橙色
                ui->key_Cap_Lock->setStyleSheet("QPushButton{background-color:rgb(250, 148, 27);}");// 橙色  rgb(35, 192, 220)
            }
            else
            {
                // 把大小写按键的背景颜色设置为白色
                ui->key_Cap_Lock->setStyleSheet("QPushButton{background-color:rgb(255, 255, 255);}");// 橙色  rgb(35, 192, 220)
            }
            //ui->key_A->setStyleSheet("QPushButton{background-color:color: rgb(250, 148, 27);}");// 橙色

            //CapLockFlag = false;
            ui->key_A->setText (shift_caplock? "A":"a");
            ui->key_B->setText (shift_caplock? "B":"b");
            ui->key_C->setText (shift_caplock? "C":"c");
            ui->key_D->setText (shift_caplock? "D":"d");
            ui->key_E->setText (shift_caplock? "E":"e");
            ui->key_F->setText (shift_caplock? "F":"f");
            ui->key_G->setText (shift_caplock? "G":"g");
            ui->key_H->setText (shift_caplock? "H":"h");
            ui->key_I->setText (shift_caplock? "I":"i");
            ui->key_J->setText (shift_caplock? "J":"j");
            ui->key_K->setText (shift_caplock? "K":"k");
            ui->key_L->setText (shift_caplock? "L":"l");
            ui->key_M->setText (shift_caplock? "M":"m");
            ui->key_N->setText (shift_caplock? "N":"n");
            ui->key_O->setText (shift_caplock? "O":"o");
            ui->key_P->setText (shift_caplock? "P":"p");
            ui->key_Q->setText (shift_caplock? "Q":"q");
            ui->key_R->setText (shift_caplock? "R":"r");
            ui->key_S->setText (shift_caplock? "S":"s");
            ui->key_T->setText (shift_caplock? "T":"t");
            ui->key_U->setText (shift_caplock? "U":"u");
            ui->key_V->setText (shift_caplock? "V":"v");
            ui->key_W->setText (shift_caplock? "W":"w");
            ui->key_X->setText (shift_caplock? "X":"x");
            ui->key_Y->setText (shift_caplock? "Y":"y");
            ui->key_Z->setText (shift_caplock? "Z":"z");
            ui->Key_Comma->setText (ShiftFlag? "<":",");
            ui->Key_Period->setText (ShiftFlag? ">":".");
            ui->Key_Slash->setText ("?");
            ui->Key_quotation->setText ("'");

        //    ui->Key_rslash->setText (ShiftFlag? "|":"\\");
        //    ui->Key_Colon->setText (ShiftFlag? ":":";");
        //    ui->key_minus->setText (ShiftFlag? "_":"-");
        //    ui->key_equal->setText (ShiftFlag? "+":"=");
        //    ui->key_braceleft->setText (ShiftFlag? "{":"[");
        //    ui->key_braceright->setText (ShiftFlag? "}":"]");
        //    ui->Key_quotation->setText (ShiftFlag? "\"":"\'");
        //    ui->Key_xx->setText (ShiftFlag? "~":"`");


        }

#if 0
        shift_caplock = CapLockFlag;
        if( shift_caplock )
        {

            CapLockFlag = false;
            ui->key_A->setText (shift_caplock? "A":"a");
            ui->key_B->setText (shift_caplock? "B":"b");
            ui->key_C->setText (shift_caplock? "C":"c");
            ui->key_D->setText (shift_caplock? "D":"d");
            ui->key_E->setText (shift_caplock? "E":"e");
            ui->key_F->setText (shift_caplock? "F":"f");
            ui->key_G->setText (shift_caplock? "G":"g");
            ui->key_H->setText (shift_caplock? "H":"h");
            ui->key_I->setText (shift_caplock? "I":"i");
            ui->key_J->setText (shift_caplock? "J":"j");
            ui->key_K->setText (shift_caplock? "K":"k");
            ui->key_L->setText (shift_caplock? "L":"l");
            ui->key_M->setText (shift_caplock? "M":"m");
            ui->key_N->setText (shift_caplock? "N":"n");
            ui->key_O->setText (shift_caplock? "O":"o");
            ui->key_P->setText (shift_caplock? "P":"p");
            ui->key_Q->setText (shift_caplock? "Q":"q");
            ui->key_R->setText (shift_caplock? "R":"r");
            ui->key_S->setText (shift_caplock? "S":"s");
            ui->key_T->setText (shift_caplock? "T":"t");
            ui->key_U->setText (shift_caplock? "U":"u");
            ui->key_V->setText (shift_caplock? "V":"v");
            ui->key_W->setText (shift_caplock? "W":"w");
            ui->key_X->setText (shift_caplock? "X":"x");
            ui->key_Y->setText (shift_caplock? "Y":"y");
            ui->key_Z->setText (shift_caplock? "Z":"z");
            ui->Key_Comma->setText (ShiftFlag? "<":",");
            ui->Key_Period->setText (ShiftFlag? ">":".");
            ui->Key_Slash->setText (ShiftFlag? "?":"/");
        //    ui->Key_rslash->setText (ShiftFlag? "|":"\\");
        //    ui->Key_Colon->setText (ShiftFlag? ":":";");
        //    ui->key_minus->setText (ShiftFlag? "_":"-");
        //    ui->key_equal->setText (ShiftFlag? "+":"=");
        //    ui->key_braceleft->setText (ShiftFlag? "{":"[");
        //    ui->key_braceright->setText (ShiftFlag? "}":"]");
            ui->Key_quotation->setText (ShiftFlag? "\"":"\'");
        //    ui->Key_xx->setText (ShiftFlag? "~":"`");

        }


#endif
#if  0
    //修改 ③
//    ui->key_0->setText (ShiftFlag? ")":"0");
//    ui->key_1->setText (ShiftFlag? "!":"1");
//    ui->key_2->setText (ShiftFlag? "@":"2");
//    ui->key_3->setText (ShiftFlag? "#":"3");
//    ui->key_4->setText (ShiftFlag? "$":"4");
//    ui->key_5->setText (ShiftFlag? "%":"5");
//    ui->key_6->setText (ShiftFlag? "^":"6");
//    ui->key_7->setText (ShiftFlag? "&&":"7");
//    ui->key_8->setText (ShiftFlag? "*":"8");
//    ui->key_9->setText (ShiftFlag? "(":"9");


    bool shift_caplock = (ShiftFlag ^ CapLockFlag);


    ui->key_A->setText (shift_caplock? "A":"a");
    ui->key_B->setText (shift_caplock? "B":"b");
    ui->key_C->setText (shift_caplock? "C":"c");
    ui->key_D->setText (shift_caplock? "D":"d");
    ui->key_E->setText (shift_caplock? "E":"e");
    ui->key_F->setText (shift_caplock? "F":"f");
    ui->key_G->setText (shift_caplock? "G":"g");
    ui->key_H->setText (shift_caplock? "H":"h");
    ui->key_I->setText (shift_caplock? "I":"i");
    ui->key_J->setText (shift_caplock? "J":"j");
    ui->key_K->setText (shift_caplock? "K":"k");
    ui->key_L->setText (shift_caplock? "L":"l");
    ui->key_M->setText (shift_caplock? "M":"m");
    ui->key_N->setText (shift_caplock? "N":"n");
    ui->key_O->setText (shift_caplock? "O":"o");
    ui->key_P->setText (shift_caplock? "P":"p");
    ui->key_Q->setText (shift_caplock? "Q":"q");
    ui->key_R->setText (shift_caplock? "R":"r");
    ui->key_S->setText (shift_caplock? "S":"s");
    ui->key_T->setText (shift_caplock? "T":"t");
    ui->key_U->setText (shift_caplock? "U":"u");
    ui->key_V->setText (shift_caplock? "V":"v");
    ui->key_W->setText (shift_caplock? "W":"w");
    ui->key_X->setText (shift_caplock? "X":"x");
    ui->key_Y->setText (shift_caplock? "Y":"y");
    ui->key_Z->setText (shift_caplock? "Z":"z");
    ui->Key_Comma->setText (ShiftFlag? "<":",");
    ui->Key_Period->setText (ShiftFlag? ">":".");
    ui->Key_Slash->setText (ShiftFlag? "?":"/");
//    ui->Key_rslash->setText (ShiftFlag? "|":"\\");
//    ui->Key_Colon->setText (ShiftFlag? ":":";");
//    ui->key_minus->setText (ShiftFlag? "_":"-");
//    ui->key_equal->setText (ShiftFlag? "+":"=");
//    ui->key_braceleft->setText (ShiftFlag? "{":"[");
//    ui->key_braceright->setText (ShiftFlag? "}":"]");
    ui->Key_quotation->setText (ShiftFlag? "\"":"\'");
//    ui->Key_xx->setText (ShiftFlag? "~":"`");

#endif
}

void keyboard::setBar (int sel, bool enable)
{
    switch(sel) {
        case 0: ui->font_1->setVisible (enable);
            ui->lineEdit->setVisible (enable);
        case 1: ui->font_2->setVisible (enable);
        case 2: ui->font_3->setVisible (enable);
        case 3: ui->font_4->setVisible (enable);
        case 4: ui->font_5->setVisible (enable);
        case 5: ui->font_6->setVisible (enable);
        case 6: ui->font_7->setVisible (enable);
        case 7: ui->font_8->setVisible (enable);
        case 8: ui->font_9->setVisible (enable);
        case 9: ui->font_10->setVisible (enable);
    }
}

void keyboard::clearBar ()
{
    setBar(0,false);
    ui->lineEdit->clear ();
    ui->lineEdit->setVisible (true);
    ui->listDown->setVisible (false);
    ui->listUp->setVisible (false);
}

void keyboard::clearBarToBtn ()//2020.11.23补充判断 修复小问题
{
    if(ui->lineEdit->isVisible())
    {
        setBar(0,false);
        ui->lineEdit->clear ();
        ui->lineEdit->setVisible (true);
        ui->listDown->setVisible (false);
        ui->listUp->setVisible (false);
    }
}

bool keyboard::transPy(QString pyInput)
{
    QByteArray strPy,strInput,xxx;
    int len,len1,comp;
    QString ddd;
    bool foundChinese;
    ui->lineEdit->insert(pyInput);
    strInput = ui->lineEdit->text ().toAscii ();
    len = strInput.count ();;

//    findNothing = true;
    if(method == wuhua) foundChinese = true;
    else foundChinese = openFile(strInput.at (0));
//    if(method == wuhua) findNothing = true;
//    else findNothing = openFile(strInput.at (0));
    if(foundChinese)
    {
      foundChinese = false;
      do
      {
           xxx = file.readLine().trimmed ();
           if(xxx.isEmpty ()) break;
           ddd.clear ();
           ddd.append (xxx);
           clist = ddd.split (" ");//, QString::SkipEmptyParts);
           strPy = clist.value (0).toAscii ();
           len1 = strPy.count ();
           if(len > len1) continue;
           comp = memcmp(strPy,strInput,len);
           if(!comp)
           {
//               findNothing = false;
               foundChinese = true;
               break;
           }
      } while(!ddd.isEmpty ());
      file.seek(0);
    }

    //计算分页数据
    pageCount = currentPage = 0;
//    if(findNothing == false)
    if(foundChinese)
    {
       int count = 0,i;
       len1 = 1;
       do
       {
           record[pageCount].start = len1;

           for(len = len1;len < clist.count ();)
           {
               clist.value(len).trimmed ();
               QByteArray data = clist.value (len).toAscii ();
               clist.value(len).replace("\n","\0");

               i = data.count () / 2;//strlen(data.data ());

               i *= 16;
               i += 9;

//               i *= 16;   // 原来的代码，修改这里可以改变输入面板显示的中文字符个数
//               i += 9;    // 原来的代码，修改这里可以改变输入面板显示的中文字符个数
               if((count + i <= 250) && (len - len1 < 9))
               {
                  if( len == clist.count() - 1 ) break;
                  count += i;
                  len ++;
               }
               else
               {
                 count = 0;
                 break;;
               }
           }
           record[pageCount].end = len;
           len1 = len + 1;
           pageCount ++;
       } while(len + 1 < clist.count ());
       //如果找到匹配的中文字符
//       findNothing = true;
       showPage(currentPage);    //显示第一页匹配的字符,从0开始
    }
    else //if(findNothing)    //如果没有匹配的中文字符
    {
        QString ss = ui->lineEdit->text ();
        clearBar  ();
        ui->lineEdit->setText (ss);
        return false;// NULL;
    }

    return true;//py;
}

void keyboard::showPage(int index)
{
    int n =0,m,left = 0,top;
    bool isValid = true;
    QPushButton *qp = NULL;
    QString str;
    left = ui->font_1->geometry ().left ();
    top = ui->font_1->geometry ().top ();
    setBar(0,true);
//    qDebug() << "start:" << record[index].start << "  end:" << record[index].end << endl;
    for(n = record[index].start; n <= record[index].end; n ++)
    {
         m = clist.value (n).count () ;// / 2;


         //
         m *= 20;
         m += 18;

         //原来的代码
//         m *= 16;
//         m += 9;
         switch(n - record[index].start)
#if 0
         {
            case 0:  ui->font_1->setGeometry (left,top,m,25);
//                     ui->font_1->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     ui->font_1->setText (tr("%1").arg ((clist.value (n))).toAscii ().trimmed ());
                       break;
            case 1:  ui->font_2->setGeometry (left,top,m,25);
                     ui->font_2->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 2:  ui->font_3->setGeometry (left,top,m,25);
                     ui->font_3->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 3:  ui->font_4->setGeometry (left,top,m,25);
                     ui->font_4->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 4:  ui->font_5->setGeometry (left,top,m,25);
                     ui->font_5->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 5:  ui->font_6->setGeometry (left,top,m,25);
                     ui->font_6->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 6:  ui->font_7->setGeometry (left,top,m,25);
                     ui->font_7->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 7:  ui->font_8->setGeometry (left,top,m,25);
                     ui->font_8->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 8:  ui->font_9->setGeometry (left,top,m,25);
                     ui->font_9->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
            case 9:  ui->font_10->setGeometry (left,top,m,25);
                     ui->font_10->setText (tr(clist.value (n).toAscii ()).trimmed ());
                     break;
         }
#else
         {
            case 0: qp = ui->font_1 ;break;
            case 1: qp = ui->font_2 ;break;
            case 2: qp = ui->font_3 ;break;
            case 3: qp = ui->font_4 ;break;
            case 4: qp = ui->font_5 ;break;
            case 5: qp = ui->font_6 ;break;
            case 6: qp = ui->font_7 ;break;
            case 7: qp = ui->font_8 ;break;
            case 8: qp = ui->font_9 ;break;
            case 9: qp = ui->font_10 ;break;
            default: isValid = false;
         }

         if(isValid)
         {
            // // 修改 25 可以改变每个中文字符候选框的高度
            qp->setGeometry (left,top,m,38);// qp->setGeometry (left,top,m,25);
            str.clear ();;
            str.append (clist.value (n));
            qp->setText (str);
         }
#endif
         left += m+2;
         //left += m;   // 原来的代码
     }
    setBar(n - record[index].start,false);


    if(pageCount > 1)  //多于一页
    {
        if(index == 0)
        {
            ui->listUp->setVisible (false);
            ui->listDown->setVisible (true);
        }
        else if((index + 1) == pageCount)
        {
            ui->listUp->setVisible (true);
            ui->listDown->setVisible (false);
        }
        else
        {
            ui->listDown->setVisible (true);
            ui->listUp->setVisible (true);
        }
    }
    else
    {
        ui->listDown->setVisible (false);
        ui->listUp->setVisible (false);
    }
//    qDebug() << "exit showPage()" << endl;
}

void keyboard::moveKeys (int diff)
{

    //修改④
//    ui->key_0->move (ui->key_0->x (),ui->key_0->y () + diff);
//    ui->key_1->move (ui->key_1->x (),ui->key_1->y () + diff);
//    ui->key_2->move (ui->key_2->x (),ui->key_2->y () + diff);
//    ui->key_3->move (ui->key_3->x (),ui->key_3->y () + diff);
//    ui->key_4->move (ui->key_4->x (),ui->key_4->y () + diff);
//    ui->key_5->move (ui->key_5->x (),ui->key_5->y () + diff);
//    ui->key_6->move (ui->key_6->x (),ui->key_6->y () + diff);
//    ui->key_7->move (ui->key_7->x (),ui->key_7->y () + diff);
//    ui->key_8->move (ui->key_8->x (),ui->key_8->y () + diff);
//    ui->key_9->move (ui->key_9->x (),ui->key_9->y () + diff);

    ui->key_A->move (ui->key_A->x (),ui->key_A->y () + diff);
    ui->key_B->move (ui->key_B->x (),ui->key_B->y () + diff);
    ui->key_C->move (ui->key_C->x (),ui->key_C->y () + diff);
    ui->key_D->move (ui->key_D->x (),ui->key_D->y () + diff);
    ui->key_E->move (ui->key_E->x (),ui->key_E->y () + diff);
    ui->key_F->move (ui->key_F->x (),ui->key_F->y () + diff);
    ui->key_G->move (ui->key_G->x (),ui->key_G->y () + diff);
    ui->key_H->move (ui->key_H->x (),ui->key_H->y () + diff);
    ui->key_I->move (ui->key_I->x (),ui->key_I->y () + diff);
    ui->key_J->move (ui->key_J->x (),ui->key_J->y () + diff);
    ui->key_K->move (ui->key_K->x (),ui->key_K->y () + diff);
    ui->key_L->move (ui->key_L->x (),ui->key_L->y () + diff);
    ui->key_M->move (ui->key_M->x (),ui->key_M->y () + diff);
    ui->key_N->move (ui->key_N->x (),ui->key_N->y () + diff);
    ui->key_O->move (ui->key_O->x (),ui->key_O->y () + diff);
    ui->key_P->move (ui->key_P->x (),ui->key_P->y () + diff);
    ui->key_Q->move (ui->key_Q->x (),ui->key_Q->y () + diff);
    ui->key_R->move (ui->key_R->x (),ui->key_R->y () + diff);
    ui->key_S->move (ui->key_S->x (),ui->key_S->y () + diff);
    ui->key_T->move (ui->key_T->x (),ui->key_T->y () + diff);
    ui->key_U->move (ui->key_U->x (),ui->key_U->y () + diff);
    ui->key_V->move (ui->key_V->x (),ui->key_V->y () + diff);
    ui->key_W->move (ui->key_W->x (),ui->key_W->y () + diff);
    ui->key_X->move (ui->key_X->x (),ui->key_X->y () + diff);
    ui->key_Y->move (ui->key_Y->x (),ui->key_Y->y () + diff);
    ui->key_Z->move (ui->key_Z->x (),ui->key_Z->y () + diff);

    ui->Key_Comma->move (ui->Key_Comma->x (),ui->Key_Comma->y () + diff); //,
    ui->Key_Period->move (ui->Key_Period->x (),ui->Key_Period->y () + diff);  //.
    ui->Key_Slash->move (ui->Key_Slash->x (),ui->Key_Slash->y () + diff);   // 斜杠
 //   ui->Key_rslash->move (ui->Key_rslash->x (),ui->Key_rslash->y () + diff); //反斜杠
 //   ui->Key_Colon->move (ui->Key_Colon->x (),ui->Key_Colon->y () + diff);  //:

    ui->key_Space->move (ui->key_Space->x (),ui->key_Space->y () + diff);
    ui->key_Enter->move (ui->key_Enter->x (),ui->key_Enter->y () + diff);
    ui->key_Backspace->move (ui->key_Backspace->x (),ui->key_Backspace->y () + diff);
    //ui->key_Escape->move (ui->key_Escape->x (),ui->key_Escape->y () + diff);
    ui->key_Cap_Lock->move (ui->key_Cap_Lock->x (),ui->key_Cap_Lock->y () + diff);
    ui->key_LangueChange->move (ui->key_LangueChange->x (),ui->key_LangueChange->y () + diff);

    //ui->Key_left->move (ui->Key_left->x (),ui->Key_left->y () + diff);
    //ui->Key_right->move (ui->Key_right->x (),ui->Key_right->y () + diff);
    //ui->Key_up->move (ui->Key_up->x (),ui->Key_up->y () + diff);
    //ui->Key_down->move (ui->Key_down->x (),ui->Key_down->y () + diff);

//    ui->key_minus->move (ui->key_minus->x (),ui->key_minus->y () + diff); //-
//    ui->key_equal->move (ui->key_equal->x (),ui->key_equal->y () + diff); //=
//    ui->key_braceleft->move (ui->key_braceleft->x (),ui->key_braceleft->y () + diff); //{
//    ui->key_braceright->move (ui->key_braceright->x (),ui->key_braceright->y () + diff); //}
    ui->Key_quotation->move (ui->Key_quotation->x (),ui->Key_quotation->y () + diff); //'
//    ui->Key_xx->move (ui->Key_xx->x (),ui->Key_xx->y () + diff);  //`
    ui->Key_Close->move (ui->Key_Close->x (),ui->Key_Close->y () + diff);
    ui->key_Shift->move (ui->key_Shift->x (),ui->key_Shift->y () + diff);

    //ui->key_Ctrl->move (ui->key_Ctrl->x (),ui->key_Ctrl->y () + diff);
    //ui->key_Alt->move (ui->key_Alt->x (),ui->key_Alt->y () + diff);
    //ui->key_Tab->move (ui->key_Tab->x (),ui->key_Tab->y () + diff);
}

void keyboard::changeSize ()
{
    QRect qrect = geometry ();
    //qrect.setHeight(method == english? 125:150);
    //qrect.setWidth (method == english? 430:420);
    setGeometry(qrect.x (),qrect.y (),(method == english? 430:420),(method == english? 125:150));//qrect);
    //setGeometry(qrect.x (),qrect.y (),(method == english? 430:420),(method == english? 125:150));//qrect);
    setBar(0,(method != english));

    // 当输入法的输入方式改变时，对键盘的布局进行改动，也就是移动控件
    //moveKeys(method == english? -25:25);
    moveKeys(0);    // 我这里修改一下，使得参数为 0 ,则不让控件的布局发生改变
}

void keyboard::changeInputMethod()
{
//    if(file.isOpen ()) file.close ();

    switch(method){
        case english:  //英文输入转拼音输入法
          method = pinying;
          ui->key_LangueChange->setText(tr("中文"));

          {      //我添加的代码,，这样子无论什么情况下按下输入法选择按键，都会回到初始状态
            ShiftFlag = true;// id == Qt::Key_Shift) && (!ShiftFlag)
            CapLockFlag = false;
            setKeyValue( Qt::Key_Shift );
          }

          setBar(0,true);
//          file.setFileName(":/src/MPY.TXT");
          changeSize();
          break;

//          暂时注释，因为要去掉多余的输入法类型
//        case pinying:  //拼音输入法转五笔输入法
//          method = wubi;
//          ui->key_LangueChange->setText (tr("五笔"));
//       //   file.setFileName(":/src/WB.TXT");
//          break;
//        case wubi:
//          method = wuhua;  //五笔输入法转五笔划输入法
//          ui->key_LangueChange->setText (tr("五划"));
//          if(file.isOpen ()) file.close ();
//          file.setFileName(":/src/WBH.TXT");
//          if( !file.open(QIODevice::ReadOnly | QIODevice::Text) )
//            QMessageBox::warning(0,tr("加载错误"),tr("无法加载字符编码库文件\n请检查文件是否有效！"));
//          break;
//        case wuhua:


        default:  //中文输入法转英文字符输入
          method = english;
          ui->key_LangueChange->setText(tr("英文"));

          {   //  我添加的代码,，这样子无论什么情况下按下输入法选择按键，都会回到初始状态
          ShiftFlag = true;// id == Qt::Key_Shift) && (!ShiftFlag)
          CapLockFlag = false;
          setKeyValue( Qt::Key_Shift );

          }
          setBar(0,false);
          changeSize();
          break;
    }

    clearBar();
    firstFlag = ' ';
    if(method == english)
//    {
//        if( !file.open(QIODevice::ReadOnly | QIODevice::Text) )
//          QMessageBox::warning(0,tr("加载错误"),tr("无法加载字符编码库文件\n请检查文件是否有效！"));
//    }
    /*else*/ ui->lineEdit->setVisible (false);
}

bool keyboard::openFile(char ch)
{
    bool retValue = true;
    char fileName[15];

    if( ((method == pinying) && ((ch == 'i') || (ch == 'u') || (ch == 'v')) )
          || ((method == wubi) && (ch == 'z')) )
    {
//    	   firstFlag = ch;
           return false;
        }
    if(ch == firstFlag) return true;

    firstFlag = ch;
    sprintf(fileName,":/src/%s%c",(method == pinying)? "py":"wb",ch);

    if(file.isOpen ()) file.close ();
    file.setFileName(fileName);
    {
        if( !file.open(QIODevice::ReadOnly | QIODevice::Text) )
        {
            retValue = false;
            QMessageBox::warning(0,tr("加载错误"),tr("无法加载字符编码库文件%1\n请检查文件是否有效！").arg (file.fileName ()));
        }
    }

    return retValue;
}

void keyboard::on_clearBtn_clicked()
{
    clearBar();
}

void keyboard::on_key_Space_clicked()
{
}
